package pers.jg.rrsserver.controller

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.google.gson.GsonBuilder
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import pers.jg.rrsserver.entity.Agritainment
import pers.jg.rrsserver.mapper.AgritainmentMapper

@RestController
@CrossOrigin
class AgritainmentController {

    val dateFormat = "yyyy-MM-dd'T'HH:mm:sssZ"

    class AgritainmentList {
        lateinit var agritainmentList: ArrayList<Agritainment>
    }

    @Autowired
    private lateinit var agritainmentMapper: AgritainmentMapper

    @PostMapping("/api/postNewAgritainment")
    fun postNewAgritainment(
        @RequestBody data: String
    ): String {

        val gson = GsonBuilder()
            .setDateFormat(dateFormat)
            .create()
        val newAgritainment = gson.fromJson(data, Agritainment::class.java)
        println(newAgritainment.id)
        val res = agritainmentMapper.insert(newAgritainment)
        return res.toString()

    }

    @GetMapping("/api/getAgritainmentList")
    fun getAgritainmentList(
        @RequestParam(value = "key", defaultValue = "") key: String,
        @RequestParam(value = "orderBy", defaultValue = "") orderBy: String
    ): String {
        val queryWrapper = QueryWrapper<Agritainment>()
        if (key != "") {
            queryWrapper.like("name", key).or().like("introduction", key)
        }
        if(orderBy == ""){
            queryWrapper.orderByDesc("add_time")   // 按照时间降序排序
        }
        else{
            queryWrapper.orderByDesc(orderBy)   // 按照给定的字段降序排序
        }
        val agritainmentList = agritainmentMapper.selectList(queryWrapper)
        val gson = GsonBuilder()
            .create()
        return gson.toJson(agritainmentList)
    }


    @PostMapping("/api/updateAgritainment")
    fun updateAgritainment(
        @RequestBody data: String
    ): String {

        val gson = GsonBuilder()
            .setDateFormat(dateFormat)
            .create()
        val agritainment = gson.fromJson(data, Agritainment::class.java)
        println(agritainment.id)
        val res = agritainmentMapper.updateById(agritainment)
        return res.toString()
    }

    @PostMapping("/api/deleteAgritainment")
    fun deleteAgritainment(
        @RequestParam("id") id: String
    ): String {
        println(id)
        return agritainmentMapper.deleteById(id).toString()
    }

    @GetMapping("/api/getAgritainment")
    fun getAgritainment(
        @RequestParam("id") id: String
    ): String {
        val agritainment = agritainmentMapper.selectById(id)
        val gson = GsonBuilder()
            .create()
        return gson.toJson(agritainment)
    }


    @GetMapping("/api/getSellerAgritainmentList")
    fun getSellerAgritainmentList(
        @RequestParam("sellerId") sellerId: Int
    ): String? {
        val queryWrapper = QueryWrapper<Agritainment>()
        queryWrapper.eq("seller_id", sellerId)
        queryWrapper.orderByDesc("add_time")
        val agritainmentList = agritainmentMapper.selectList(queryWrapper)
        val gson = GsonBuilder()
            .create()
        return gson.toJson(agritainmentList)
    }
}